//
//  CZDetailHeaderView.swift
//  GZWeibo007
//
//  Created by newuser on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

///头部view的代理
protocol CZDetailHeaderViewDelegate: NSObjectProtocol {
    ///按点击点击调用
    func detailHeaderViewClick(headerView: UIView)
}



class CZDetailHeaderView: UIView {

    //代理属性
    weak var delegate: CZDetailHeaderViewDelegate?
    
    
    ///微博模型
    var status: CZStatus? {
        didSet {
        setButton(commentButton, title: "评论 ", count: status!.comments_count)
        }
    }
    
    
    private func setButton(button:UIButton,title:String,count:Int) {
    
        if count >= 10000 {
        
            let final = CGFloat(count) / 10000.0
            var titleTxet = String(format: "%.1f万", arguments: [final])
           titleTxet = titleTxet.stringByReplacingOccurrencesOfString(".0", withString: "")
            button.setTitle(title + " " + titleTxet, forState: UIControlState.Normal)
        }else {
        
        button.setTitle("\(title)\(count)", forState: UIControlState.Normal)
        }
    
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.98, alpha: 1)
        //准备UI
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//MARK: - 按钮点击事件
    func commentButtonClick(button: UIButton)
    {
       delegate?.detailHeaderViewClick(button)
    }
    
    
    

//MARK - 添加UI
    func prepareUI() {
        //添加子控件
        addSubview(commentButton)
        //添加约束
        commentButton.ff_AlignInner(type: ff_AlignType.CenterLeft, referView: self, size: CGSize(width: 70, height: 44), offset: CGPoint(x: 10, y: 0))
    }
    
//MARK: - 懒加载控件
    private lazy var commentButton: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "commentButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("评论", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(11)
        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Highlighted)
        
       return button
    }()
    

    
}
